package com.wgchao.identityrightsmanagement.query;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseDepartment;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseDepartmentRepository;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.query.pageparam.DepartmentPageParam;
import com.wgchao.identityrightsmanagement.query.vo.AccountVo;
import com.wgchao.identityrightsmanagement.query.vo.DepartmentVo;
import com.wgchao.identityrightsmanagement.query.vo.DepartmentVoConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: DepartmentQuery
 * @author: wgchao
 * @createTime: 2020/6/16 11:40 AM
 * @Description: TODO
 */
@Service
public class DepartmentQuery {
    @Autowired
    private DepartmentVoConvert departmentVoConvert;
    @Autowired
    private SysBaseDepartmentRepository sysBaseDepartmentRepository;

    /**
     * 查询部门基本信息
     * @param code
     * @return
     */
    public DepartmentVo findByCode(String code) {
        SysBaseDepartment sysBaseDepartment = sysBaseDepartmentRepository.findFirstByCodeAndDelIsFalse(code);
        if(sysBaseDepartment == null){
            return null;
        }
        return departmentVoConvert.convert(sysBaseDepartment);
    }

    /**
     * 条件分页查询部门信息
     * @param departmentPageParam
     * @return
     */
    public PageData<DepartmentVo> findByConditions(DepartmentPageParam departmentPageParam) {
        Specification<SysBaseDepartment> specification = new Specification<SysBaseDepartment>() {
            @Override
            public Predicate toPredicate(Root<SysBaseDepartment> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isEmpty(departmentPageParam.getAgencyCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("agencyCode"), departmentPageParam.getAgencyCode());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(departmentPageParam.getDname())){
                    Predicate predicate = criteriaBuilder.equal(root.get("name"), departmentPageParam.getDname());
                    predicateList.add(predicate);
                }
                if(departmentPageParam.getDisabled() != null){
                    if(departmentPageParam.getDisabled()){
                        Predicate predicate = criteriaBuilder.isTrue(root.get("disabled"));
                        predicateList.add(predicate);
                    }else{
                        Predicate predicate = criteriaBuilder.isFalse(root.get("disabled"));
                        predicateList.add(predicate);
                    }
                }
                Predicate predicate = criteriaBuilder.isFalse(root.get("del"));
                predicateList.add(predicate);

                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };
        Pageable pageable = PageRequest.of(departmentPageParam.getCurr() - 1, departmentPageParam.getLimit(), Sort.by(Sort.Order.desc("createTime")));

        Page<SysBaseDepartment> departmentPage = sysBaseDepartmentRepository.findAll(specification, pageable);

        if(departmentPage.isEmpty()) {
            return new PageData<>(0L, departmentPageParam.getCurr(), null);
        }
        List<DepartmentVo> departmentVoList = departmentPage.get().map(sysBaseDepartment -> {
            return departmentVoConvert.convert(sysBaseDepartment);
        }).collect(Collectors.toList());

        return new PageData<>(departmentPage.getTotalElements(), departmentPageParam.getCurr(), departmentVoList);
    }
}
